package com.yywq.controller;

import com.yywq.mapper.UserMapper;
import com.yywq.pojo.User;
import com.yywq.response.CommonReturnType;
import com.yywq.service.impl.GoodPictureServiceImpl;
import com.yywq.utils.FTPUtil;
import com.yywq.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: 吴磊
 * @program: yywq_parent
 * @create: 2021-04-16 20:58
 */
@RestController
@RequestMapping("/upload")
public class UploadPictures {
    @Autowired
    private FTPUtil ftpUtil;

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GoodPictureServiceImpl goodPictureService;

    /**
     * 上传用户头像
     *
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "user", method = RequestMethod.POST)
    private CommonReturnType uploadUserPic(MultipartFile file) throws Exception {
        String userId = myUtil.getUserToken().getId();
        String filePath = ftpUtil.uploadFolder(file);
        User user = userMapper.getUserById(userId);
        userMapper.updateUserPicture(user.getTelPhone(), filePath);
        return CommonReturnType.create(null);
    }


    /**
     * 上传商品图片
     *
     * @param gid
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "goods", method = RequestMethod.POST)
    private CommonReturnType uploadGoodPic(@RequestParam("gid") String gid, MultipartFile file) throws Exception {
        String filePath = ftpUtil.uploadFolder(file);
        goodPictureService.insertGoodsPicture(gid, filePath);
        return CommonReturnType.create(null);
    }
}
